package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.Information;
import com.example.demo.exception.bizException.BizException;
import com.example.demo.mapper.InformationMapper;
import com.example.demo.service.InformationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
* <p>
*  服务实现类
* </p>
*
* @author cy
* @since 2020-08-16
*/
@Slf4j
@Service
public class InformationServiceImpl extends ServiceImpl<InformationMapper, Information> implements InformationService {

    @Override
    public Page<Information> listInformationsByPage(int page, int pageSize, Integer userId) {
        log.info("正在执行分页查询information: page = {} pageSize = {} userId = {}",page,pageSize,userId);
        QueryWrapper<Information> queryWrapper =  new QueryWrapper<Information>().eq("user_id",userId).orderByDesc("time");
        //TODO 这里需要自定义用于匹配的字段,并把wrapper传入下面的page方法
        Page<Information> result = super.page(new Page<>(page, pageSize),queryWrapper);
        log.info("分页查询information完毕: 结果数 = {} ",result.getRecords().size());
        return result;
    }

    @Override
    public Information getInformationById(int id) {
        log.info("正在查询information中id为{}的数据",id);
        Information information = super.getById(id);
        log.info("查询id为{}的information{}",id,(null == information?"无结果":"成功"));
        if(information==null){
            return null;
        }
        //设置已读
        if(information.getState().equals(Information.NO_READ)){
            information.setState(Information.READ);
            super.updateById(information);
        }
        return information;
    }

    @Override
    public int insertInformation(Information information) {
        log.info("正在插入information");
        information.setTime(LocalDateTime.now());
        if (super.save(information)) {
            log.info("插入information成功,id为{}",information.getInformationId());
            return information.getInformationId();
        } else {
            log.error("插入information失败");
            throw new BizException("添加失败");
        }
    }

    @Transactional
    @Override
    public int deleteInformationById(int id,int userId) {
        log.info("正在删除id为{}的information",id);
        Information information=super.getById(id);
        if(information.getUserId()!=userId){
            throw new BizException("该通知不是"+userId+"用户的");
        }
        if (super.removeById(id)) {
            log.info("删除id为{}的information成功",id);
            return id;
        } else {
            log.error("删除id为{}的information失败",id);
            throw new BizException("删除失败[id=" + id + "]");
        }
    }

    @Transactional
    @Override
    public boolean updateInformation(Integer userId) {
        log.info("id为{}的用户已读所有information",userId);
        QueryWrapper<Information> queryWrapper=new QueryWrapper<Information>().eq("user_id",userId);
        Information information=new Information();
        information.setState(Information.READ);
        return super.update(information,queryWrapper);
    }

    @Transactional
    @Override
    public int updateInformationById(Integer userId,Integer id) {
        log.info("正在更新id为{}的information",id);
        Information information = super.getOne(new QueryWrapper<Information>().eq("information_id",id));
        //此条消息是这个用户的
        if (information.getUserId().equals(userId)){
            Information information1 = new Information();
            information1.setInformationId(id);
            information1.setState(Information.READ);
            if (super.updateById(information1)) {
                log.info("更新id为{}的information成功",id);
                return id;
            } else {
                log.error("更新id为{}的information失败",id);
                throw new BizException("更新失败[id=" + id + "]");
            }
        }else {
            throw new BizException("无权限操作此条消息");
        }
    }

}
